package com.plwf.lake.service.impl;

import com.plwf.lake.entity.Subject;
import com.plwf.lake.mapper.SubjectMapper;
import com.plwf.lake.service.SubjectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;;import java.sql.SQLException;
import java.util.List;

@Service
public class SubjectServiceImpl implements SubjectService{

    @Resource
    private SubjectMapper mapper;
    private static final Logger log = LoggerFactory.getLogger(Subject.class);

    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackFor=SQLException.class)
    public boolean save(Subject bean){
        //实体类subId不能为int，应该改为Integer，因为int 不能为空
        if(bean.getSubId()!= null){
            return this.mapper.update(bean);
        }else{
            return this.mapper.insert(bean);
        }
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,rollbackFor=SQLException.class)
    public boolean remove(Integer subId) {

        boolean flag = false;
        try{
            this.mapper.delete(subId);
        }catch(Exception ex){
            log.error(ex.getMessage());
        }
        return false;
    }

    @Override
    public Subject find(Integer subId) {

        return this.mapper.selectOne(subId);
    }

    @Override
    public List<Subject> query() {
        return this.mapper.select();
    }


}
